set     more off
set     matsize 800
clear   all
global  dpath = "[Userpath]\data and code"

use "$dpath\data\deathratecontrol.dta",clear
cd "$dpath\results"

winsor2 lifeexpectancy, replace cuts(1 99) trim //  Remove outliers

********************************************************************************
*********************************Table******************************************
********************************************************************************
*1.Table III
reghdfe totaldigestivedr auto, absorb(code year) cluster(citycode)
estimates store reg1
reghdfe totaldigestivedr auto manual lngdp lnbed, absorb(code year) cluster(citycode)
estimates store reg2
reghdfe lifeexpectancy auto, absorb(code year) cluster(citycode)
estimates store reg3
reghdfe lifeexpectancy auto manual lngdp lnbed, absorb(code year) cluster(citycode)
estimates store reg4
esttab reg1 reg2 reg3 reg4 using TableIII.rtf,replace b(%9.3f) se scalar(N r2_a) star(* 0.1 **  0.05  *** 0.01) title(Table III) keep(auto manual)
est clear


*2.Table V
reghdfe totaldigestivedr auto upstreamauto, absorb(code year) cluster(citycode)
estimates store reg1
reghdfe totaldigestivedr auto upstreamauto manual lngdp lnbed, absorb(code year) cluster(citycode)
estimates store reg2
reghdfe lifeexpectancy auto upstreamauto, absorb(code year) cluster(citycode)
estimates store reg3
reghdfe lifeexpectancy auto upstreamauto manual lngdp lnbed, absorb(code year) cluster(citycode)
estimates store reg4
esttab reg1 reg2 reg3 reg4 using TableV.rtf,replace b(%9.3f) se scalar(N r2_a) star(* 0.1 **  0.05  *** 0.01) title(Table V) keep(auto upstreamauto)
est clear

*3.Table VI
reghdfe accidentdeathrate auto, absorb(code year) cluster(citycode)
estimates store reg1
reghdfe accidentdeathrate auto manual lngdp lnbed, absorb(code year) cluster(citycode)
estimates store reg2
reghdfe lungcancerdr auto, absorb(code year) cluster(citycode)
estimates store reg3
reghdfe lungcancerdr auto manual lngdp lnbed, absorb(code year) cluster(citycode)
estimates store reg4
reghdfe respiratorydr auto, absorb(code year) cluster(citycode)
estimates store reg5
reghdfe respiratorydr auto manual lngdp lnbed, absorb(code year) cluster(citycode)
estimates store reg6
replace cardiovasculardr=. if code==450302 | code==130702 | code==220102 | code==222405 | code==360102 //  Remove outliers
reghdfe cardiovasculardr auto, absorb(code year) cluster(citycode)
estimates store reg7
reghdfe cardiovasculardr auto manual lngdp lnbed , absorb(code year) cluster(citycode)
estimates store reg8
reghdfe other auto, absorb(code year) cluster(citycode)
estimates store reg9
reghdfe other auto manual lngdp lnbed , absorb(code year) cluster(citycode)
estimates store reg10
esttab reg1 reg2 reg3 reg4 reg5 reg6 reg7 reg8 reg9 reg10 using TableVI.rtf,replace b(%9.3f) se scalar(N r2_a) star(* 0.1 **  0.05  *** 0.01) title(Table VI) keep(auto)
est clear

*4.Table VII
reghdfe digestivecancerdr auto, absorb(code year) cluster(citycode)
estimates store reg1
reghdfe digestivecancerdr auto manual lngdp lnbed, absorb(code year) cluster(citycode)
estimates store reg2
reghdfe nocancerdigestivedr auto, absorb(code year) cluster(citycode)
estimates store reg3
reghdfe nocancerdigestivedr auto manual lngdp lnbed, absorb(code year) cluster(citycode)
estimates store reg4
esttab reg1 reg2 reg3 reg4 using TableVII.rtf,replace b(%9.3f) se scalar(N r2_a) star(* 0.1 **  0.05  *** 0.01) title(Table VII) keep(auto)
est clear

*5.Table VIII
reghdfe totaldigestivedr auto if tapoveraverage==0, absorb(code urban#year) cluster(citycode)
estimates store reg1
reghdfe totaldigestivedr auto manual lngdp lnbed if tapoveraverage==0, absorb(code urban#year) cluster(citycode)
estimates store reg2
reghdfe totaldigestivedr auto if tapoveraverage==1, absorb(code urban#year) cluster(citycode)
estimates store reg3
reghdfe totaldigestivedr auto manual lngdp lnbed if tapoveraverage==1, absorb(code urban#year) cluster(citycode)
estimates store reg4
esttab reg1 reg2 reg3 reg4 using TableVIII.rtf,replace b(%9.3f) se scalar(N r2_a) star(* 0.1 **  0.05  *** 0.01) title(Table VIII) keep(auto)
est clear


*6.Table AIX Column 2/3
reghdfe totaldigestivedr auto, absorb(code year) cluster(provinceid)
estimates store reg1
reghdfe lifeexpectancy auto, absorb(code year) cluster(provinceid)
estimates store reg2
esttab reg1 reg2 using TableAIX.rtf,append b(%9.3f) se scalar(N) star(* 0.1 **  0.05  *** 0.01) title(Table AIX Column 2/3) keep(auto)
est clear

*7.Table AIX Column 8/9
replace autoyear=0 if autoyear==3300
csdid totaldigestivedr ,ivar(code) time(year) gvar(autoyear) method(drimp) cluster(provinceid) notyet agg(simple)
estimates store reg1
csdid lifeexpectancy ,ivar(code) time(year) gvar(autoyear) method(drimp) cluster(provinceid) notyet agg(simple)
estimates store reg2
esttab reg1 reg2 using TableAIX.rtf,append b(%9.3f) se scalar(N r2_a) star(* 0.1 **  0.05  *** 0.01) title(Table AIX Column 8/9)
est clear


********************************************************************************
*********************************Figure*****************************************
********************************************************************************
*8.Figure VII
reghdfe totaldigestivedr sixyearsbefore fiveyearsbefore fouryearsbefore threeyearsbefore twoyearsbefore oneyearsbefore yearofauto oneyearsafter twoyearsafter threeyearsafter fouryearsafter fiveyearsafter sixyearsafter sevenyearsafter eightmoreyearsafter manual lngdp lnbed, absorb(code year) cluster(citycode)
estimates store Dynamic
coefplot Dynamic, keep(sixyearsbefore fiveyearsbefore fouryearsbefore threeyearsbefore twoyearsbefore oneyearsbefore yearofauto oneyearsafter twoyearsafter threeyearsafter fouryearsafter fiveyearsafter sixyearsafter) ///
		vertical  recast(connect) yline(0) ///
		coeflabels(sixyearsbefore = "-6" ///
				   fiveyearsbefore = "-5"  ///
				   fouryearsbefore = "-4" ///
				   threeyearsbefore = "-3"  ///
				   twoyearsbefore = "-2" ///
				   oneyearsbefore = "-1" ///
				   yearofauto = "0"  ///
				   oneyearsafter = "1" ///
				   twoyearsafter = "2"  ///
				   threeyearsafter = "3" ///
				   fouryearsafter = "4" ///
				   fiveyearsafter = "5"  ///
				   sixyearsafter = "6" ///
				   sevenyearsafter = "7"  ///
				   eightmoreyearsafter = "≥8") ///
		ytitle("Estimated Coefficients")   xtitle("Number of years since the establishment of Automatic Stations") ///
		ciopts(recast(rcap) msize(medium))
graph save "Graph" "Figure VII.gph" ,replace


*9.Figure VIII
reghdfe lifeexpectancy sixyearsbefore fiveyearsbefore fouryearsbefore threeyearsbefore twoyearsbefore oneyearsbefore yearofauto oneyearsafter twoyearsafter threeyearsafter fouryearsafter fiveyearsafter sixyearsafter sevenyearsafter eightmoreyearsafter manual lngdp lnbed, absorb(code year) cluster(citycode)
estimates store Dynamic
coefplot Dynamic, keep(sixyearsbefore fiveyearsbefore fouryearsbefore threeyearsbefore twoyearsbefore oneyearsbefore ///
					   yearofauto ///
					   oneyearsafter twoyearsafter threeyearsafter fouryearsafter fiveyearsafter sixyearsafter) ///
		vertical  recast(connect) yline(0) ///
		coeflabels(sixyearsbefore = "-6" ///
				   fiveyearsbefore = "-5" ///
				   fouryearsbefore = "-4" ///
				   threeyearsbefore = "-3" ///
				   twoyearsbefore = "-2" ///
				   oneyearsbefore = "-1" ///
				   yearofauto = "0" ///
				   oneyearsafter = "1" ///
				   twoyearsafter = "2" ///
				   threeyearsafter = "3" ///
				   fouryearsafter = "4" ///
				   fiveyearsafter = "5"  ///
				   sixyearsafter = "6" ///
				   sevenyearsafter = "7"  ///
				   eightmoreyearsafter = "≥8")    ///
		ytitle("Estimated Coefficients")   xtitle("Number of years since the establishment of Automatic Stations") ///
		ciopts(recast(rcap) msize(medium))
graph save "Graph" "Figure VIII.gph" ,replace


*10.Figure X Upper
reghdfe digestivecancerdr sixyearsbefore fiveyearsbefore fouryearsbefore threeyearsbefore twoyearsbefore oneyearsbefore yearofauto oneyearsafter twoyearsafter threeyearsafter fouryearsafter fiveyearsafter sixyearsafter sevenyearsafter eightmoreyearsafter manual lngdp lnbed, absorb(code year) cluster(citycode)
estimates store Dynamic
coefplot Dynamic, keep(sixyearsbefore fiveyearsbefore fouryearsbefore threeyearsbefore twoyearsbefore oneyearsbefore ///
					   yearofauto ///
					   oneyearsafter twoyearsafter threeyearsafter fouryearsafter fiveyearsafter sixyearsafter) ///
		vertical  recast(connect) yline(0) ///
		coeflabels(sixyearsbefore = "-6" ///
				   fiveyearsbefore = "-5"  ///
				   fouryearsbefore = "-4" ///
				   threeyearsbefore = "-3"  ///
				   twoyearsbefore = "-2" ///
				   oneyearsbefore = "-1" ///
				   yearofauto = "0"  ///
				   oneyearsafter = "1" ///
				   twoyearsafter = "2"  ///
				   threeyearsafter = "3" ///
				   fouryearsafter = "4" ///
				   fiveyearsafter = "5"  ///
				   sixyearsafter = "6")  ///
				   ytitle("Estimated Coefficients")   xtitle("Number of years since the establishment of Automatic Stations") ///
				   ciopts(recast(rcap) msize(medium))
graph save "Graph" "Figure X Upper.gph" ,replace

*11.Figure X Lower
reghdfe nocancerdigestivedr sixyearsbefore fiveyearsbefore fouryearsbefore threeyearsbefore twoyearsbefore oneyearsbefore yearofauto oneyearsafter twoyearsafter threeyearsafter fouryearsafter fiveyearsafter sixyearsafter sevenyearsafter eightmoreyearsafter manual lngdp lnbed, absorb(code year) cluster(citycode)
estimates store Dynamic
coefplot Dynamic, keep(sixyearsbefore fiveyearsbefore fouryearsbefore threeyearsbefore twoyearsbefore oneyearsbefore ///
					   yearofauto ///
					   oneyearsafter twoyearsafter threeyearsafter fouryearsafter fiveyearsafter sixyearsafter) ///
		 vertical  recast(connect) yline(0) ///
		 coeflabels(sixyearsbefore = "-6" ///
					fiveyearsbefore = "-5"  ///
					fouryearsbefore = "-4" ///
					threeyearsbefore = "-3" ///
					twoyearsbefore = "-2" ///
					oneyearsbefore = "-1" ///
					yearofauto = "0"  ///
					oneyearsafter = "1" ///
					twoyearsafter = "2"  ///
					threeyearsafter = "3" ///
					fouryearsafter = "4" ///
					fiveyearsafter = "5"  ///
					sixyearsafter = "6") ///
		 ytitle("Estimated Coefficients")   xtitle("Number of years since the establishment of Automatic Stations") ///
		 ciopts(recast(rcap) msize(medium))
graph save "Graph" "Figure X Lower.gph" ,replace
